<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

//declare (strict_types = 1);

namespace app\home\controller;

use app\home\BaseController;
use app\admin\model\User as UserList;
use app\admin\model\WckcStudent as WckcStudentModel;
use think\facade\Db;
use think\facade\View;

class WckcStudent extends BaseController
{
    
    public function index()
    {
        //if (request()->isAjax()) {
        if (request()->get()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['name|code', 'like', '%' . $param['keywords'] . '%'];
            }

            //按时间检索
            // $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
            // $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;

            // if ($start_time > 0 && $end_time > 0) {
            //     if ($start_time === $end_time) {
            //         $where[] = ['register_time', '=', $start_time];
            //     } else {
            //         $where[] = ['register_time', '>=', $start_time];
            //         $where[] = ['register_time', '<=', $end_time];
            //     }
            // } elseif ($start_time > 0 && $end_time == 0) {
            //     $where[] = ['register_time', '>=', $start_time];
            // } elseif ($start_time == 0 && $end_time > 0) {
            //     $where[] = ['register_time', '<=', $end_time];
            // }


            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            
            $content = WckcStudentModel::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])
                ->each(function ($item, $key) {
                    $item->course=Db::name('WckcCourse')->where(['id'=>$item->cid])->value('cname');
                      $item->teacher=Db::name('WckcCourse')->where(['id'=>$item->cid])->value('teacher');
                     // $item->update_time = empty($item->update_time) ? '-' : date('Y-m-d H:i', $item->update_time);
                      // $item->create_time = empty($item->create_time) ? '-' : date('Y-m-d H:i', $item->create_time);

                 });
                dump($content);die;

            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }
    public function index2()
    {
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        $userInfo['showname'] = empty($userInfo['nickname']) ? $userInfo['username'] : $userInfo['nickname'];
        $userInfo['level_title'] = Db::name('UserLevel')->where(['id' => $userInfo['level']])->value('title');
        $userInfo['sex'] = ($userInfo['sex'] == 1) ? '男' : '女';
        add_user_log('view', '个人中心');
        View::assign('userInfo', $userInfo);
        return view();
    }

    public function info_edit()
    {
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
		$userInfo['birthday'] = $userInfo['birthday']==0 ? '' : date('Y-m-d', $userInfo['birthday']);
        add_user_log('view', '个人信息');
        View::assign('userInfo', $userInfo);
        return view();
    }

    public function edit_submit()
    {
        $param = get_params();
        $param['birthday'] = strtotime($param['birthday']);
		$param['update_time'] = time();
        $res = Db::name('User')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
        if ($res !== false) {
            add_user_log('edit', '个人信息', $param['id'], $param);
            to_assign(0, '操作成功');
        } else {
            to_assign(1, '操作失败');
        }
    }
    public function content(){
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['nickname|username|name|mobile|province|city', 'like', '%' . $param['keywords'] . '%'];
            }

            //按时间检索
            $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
            $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;

            if ($start_time > 0 && $end_time > 0) {
                if ($start_time === $end_time) {
                    $where[] = ['register_time', '=', $start_time];
                } else {
                    $where[] = ['register_time', '>=', $start_time];
                    $where[] = ['register_time', '<=', $end_time];
                }
            } elseif ($start_time > 0 && $end_time == 0) {
                $where[] = ['register_time', '>=', $start_time];
            } elseif ($start_time == 0 && $end_time > 0) {
                $where[] = ['register_time', '<=', $end_time];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            
            $content = WckcStudentModel::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])
                ->each(function ($item, $key) {
                     $item->register_time = empty($item->register_time) ? '-' : date('Y-m-d H:i', $item->register_time);
                     $item->level_name = Db::name('UserLevel')->where(['id' => $item->level])->value('title');
                 });

            dump($content);
                        $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['nickname|username|name|mobile|province|city', 'like', '%' . $param['keywords'] . '%'];
            }

            //按时间检索
            $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
            $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;

            if ($start_time > 0 && $end_time > 0) {
                if ($start_time === $end_time) {
                    $where[] = ['register_time', '=', $start_time];
                } else {
                    $where[] = ['register_time', '>=', $start_time];
                    $where[] = ['register_time', '<=', $end_time];
                }
            } elseif ($start_time > 0 && $end_time == 0) {
                $where[] = ['register_time', '>=', $start_time];
            } elseif ($start_time == 0 && $end_time > 0) {
                $where[] = ['register_time', '<=', $end_time];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];

            $content2 = UserList::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])
                ->each(function ($item, $key) {
                    $item->register_time = empty($item->register_time) ? '-' : date('Y-m-d H:i', $item->register_time);
                    $item->level_name = Db::name('UserLevel')->where(['id' => $item->level])->value('title');
                });
                 dump($content2);
    }
   
}
